package com.yanqu.road.entity.activity.goldautumn;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.goldautumn.config.GoldAutumnBuffInfo;
import com.yanqu.road.entity.activity.goldautumn.config.GoldAutumnLatticeInfo;
import com.yanqu.road.entity.activity.goldautumn.config.GoldAutumnLevelInfo;
import com.yanqu.road.entity.activity.goldautumn.config.GoldAutumnScreedInfo;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;

public class GoldAutumnConfig {

    ActivityInfo activityInfo;

    public GoldAutumnConfig(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    /**
     * 月桂币--积分兑换道具
     */
    private int SCORE_ITEM_ID;

    /**
     * 桂花酿——体力道具
     */
    private int ENERGY_ITEM_ID;

    /**
     * 所有大奖拿完后的固定循环模板
     */
    private String END_SCREED;

    /**
     * 大奖格子;道具1id=数量|大奖格子;道具1id=数量
     */
    private String RED_LATTICE;

    /**
     * 一点桂花酿获得多少月桂币
     */
    private int SCORE_ITEM_NUMBER;

    /**
     * 多少格开始可以使用自动模式（指走完第30格）
     */
    private int AUTUMN_AUTO_OPEN;

    List<GoldAutumnBuffInfo> buffInfoList;

    List<GoldAutumnLatticeInfo> latticeInfoList;

    List<GoldAutumnLevelInfo> levelInfoList;

    List<GoldAutumnScreedInfo> screedInfoList;

    public GoldAutumnScreedInfo randomGoldAutumnScreedInfo(int max, Random random){
        List<GoldAutumnScreedInfo> resultList = new ArrayList<>();
        List<Integer> weightList = new ArrayList<>();
        for (GoldAutumnScreedInfo screedInfo : screedInfoList) {
            if(max >= screedInfo.getRangeList().get(0) && max < screedInfo.getRangeList().get(1)) {
                weightList.add(screedInfo.getWeight());
                resultList.add(screedInfo);
            }
        }
        if(resultList.size() == 0){
            //获取最大值
            int lastMax = screedInfoList.get(screedInfoList.size() - 1).getMax();
            for (GoldAutumnScreedInfo screedInfo : screedInfoList) {
                if(lastMax == screedInfo.getMax()) {
                    weightList.add(screedInfo.getWeight());
                    resultList.add(screedInfo);
                }
            }
        }
        int idx = RandomHelper.getRandomIndexByWeight(weightList, random);
        return resultList.get(idx);
    }

    public GoldAutumnLatticeInfo getGoldAutumnLatticeInfo(int id){
        for (GoldAutumnLatticeInfo latticeInfo : latticeInfoList) {
            if(latticeInfo.getId() == id){
                return latticeInfo;
            }
        }
        return null;
    }

    private List<Integer> redCellIdList;

    private Map<Integer, String> redRewardMap;

    public void initRedConfig(){
        List<String> list = StringUtils.stringToStringList(getRED_LATTICE(), "\\|");
        Map<Integer, String> redRewardMap = new HashMap<>();
        List<Integer> redRewardId = new ArrayList<>();
        for (String redConfigParam : list) {
            List<String> paramList = StringUtils.stringToStringList(redConfigParam, ";");
            int cellId = Integer.parseInt(paramList.get(0));
            String reward = paramList.get(1);
            redRewardMap.put(cellId, reward);
            redRewardId.add(cellId);
        }
        this.redCellIdList = redRewardId;
        this.redRewardMap = redRewardMap;
    }

    public GoldAutumnLatticeInfo randomLattice(int stage, int colorType) {
        Random random = new Random();
        List<GoldAutumnLatticeInfo> list = getGoldAutumnLatticeInfoByColorType(stage, colorType);
        if(list.size() == 1){
            return list.get(0);
        }
        if(list.size() == 0) {
            return null;
        }
        List<Integer> weightList = new ArrayList<>();
        for (GoldAutumnLatticeInfo info : list) {
            weightList.add(info.getWeight());
        }
        int idx = RandomHelper.getRandomIndexByWeight(weightList, random);
        return list.get(idx);
    }

    public List<GoldAutumnLatticeInfo> getGoldAutumnLatticeInfoByColorType(int stage, int colorType){
        List<GoldAutumnLatticeInfo> list = new ArrayList<>();
        for (GoldAutumnLatticeInfo latticeInfo : latticeInfoList) {
            if(latticeInfo.getColorType() == colorType && latticeInfo.getStage() == stage && latticeInfo.getWeight() > 0){
                list.add(latticeInfo);
            }
        }
        return list;
    }

    public GoldAutumnBuffInfo randomGoldAutumnBuffInfo(Random random){
        List<Integer> weightList = new ArrayList<>();
        for (GoldAutumnBuffInfo buffInfo : buffInfoList) {
            weightList.add(buffInfo.getWeight());
        }
        int idx = RandomHelper.getRandomIndexByWeight(weightList, random);
        return buffInfoList.get(idx);
    }

    public int getSCORE_ITEM_ID() {
        return SCORE_ITEM_ID;
    }

    public void setSCORE_ITEM_ID(int SCORE_ITEM_ID) {
        this.SCORE_ITEM_ID = SCORE_ITEM_ID;
    }

    public int getENERGY_ITEM_ID() {
        return ENERGY_ITEM_ID;
    }

    public void setENERGY_ITEM_ID(int ENERGY_ITEM_ID) {
        this.ENERGY_ITEM_ID = ENERGY_ITEM_ID;
    }

    public String getRED_LATTICE() {
        return RED_LATTICE;
    }

    public void setRED_LATTICE(String RED_LATTICE) {
        this.RED_LATTICE = RED_LATTICE;
    }

    public List<GoldAutumnBuffInfo> getBuffInfoList() {
        return buffInfoList;
    }

    public void setBuffInfoList(List<GoldAutumnBuffInfo> buffInfoList) {
        this.buffInfoList = buffInfoList;
    }

    public List<GoldAutumnLatticeInfo> getLatticeInfoList() {
        return latticeInfoList;
    }

    public void setLatticeInfoList(List<GoldAutumnLatticeInfo> latticeInfoList) {
        this.latticeInfoList = latticeInfoList;
    }

    public List<GoldAutumnLevelInfo> getLevelInfoList() {
        return levelInfoList;
    }

    public void setLevelInfoList(List<GoldAutumnLevelInfo> levelInfoList) {
        this.levelInfoList = levelInfoList;
    }

    public List<GoldAutumnScreedInfo> getScreedInfoList() {
        return screedInfoList;
    }

    public void setScreedInfoList(List<GoldAutumnScreedInfo> screedInfoList) {
        this.screedInfoList = screedInfoList;
    }

    public List<Integer> getRedCellIdList() {
        return redCellIdList;
    }

    public void setRedCellIdList(List<Integer> redCellIdList) {
        this.redCellIdList = redCellIdList;
    }

    public Map<Integer, String> getRedRewardMap() {
        return redRewardMap;
    }

    public void setRedRewardMap(Map<Integer, String> redRewardMap) {
        this.redRewardMap = redRewardMap;
    }

    public String getEND_SCREED() {
        return END_SCREED;
    }

    public void setEND_SCREED(String END_SCREED) {
        this.END_SCREED = END_SCREED;
    }

    public int getSCORE_ITEM_NUMBER() {
        return SCORE_ITEM_NUMBER;
    }

    public void setSCORE_ITEM_NUMBER(int SCORE_ITEM_NUMBER) {
        this.SCORE_ITEM_NUMBER = SCORE_ITEM_NUMBER;
    }

    public int getAUTUMN_AUTO_OPEN() {
        return AUTUMN_AUTO_OPEN;
    }

    public void setAUTUMN_AUTO_OPEN(int AUTUMN_AUTO_OPEN) {
        this.AUTUMN_AUTO_OPEN = AUTUMN_AUTO_OPEN;
    }
}
